package main

import (
	"fmt"
	"github.com/pion/turn/v3"
	"log"
	"net"
)

func main() {
	host := "111.229.207.234:3478"
	username := "beesoft"
	password := "beesoft"
	//peer := "127.0.0.1:8080"
	peer := "111.229.207.234:8081"
	data := []byte("TEST DATA")

	addr, err := net.ResolveUDPAddr("udp", ":8081")
	if err != nil {
		panic(err)
	}

	conn, err := net.ListenUDP("udp", addr)
	if err != nil {
		panic(err)
	}

	client, err := turn.NewClient(&turn.ClientConfig{
		STUNServerAddr: host,
		TURNServerAddr: host,
		Username:       username,
		Password:       password,
		//Realm          string
		//Software       string
		//RTO            time.Duration
		Conn: conn,
		//Net            transport.Net
		//LoggerFactory  logging.LoggerFactory
	})
	if err != nil {
		panic(err)
	}

	err = client.Listen()
	if err != nil {
		log.Fatal("Failed to connect to TURN backend:", err)
	}
	defer client.Close()

	udpAddr, err := net.ResolveUDPAddr("udp", peer)
	if err != nil {
		panic(err)
	}

	//ipAddr, err := net.ResolveIPAddr("ip", "127.0.0.1")
	//if err != nil {
	//	panic(err)
	//}

	resp, err := client.WriteTo(data, udpAddr)
	if err != nil {
		panic(err)
	}

	fmt.Println("Response from backend:", resp)
}
